package com.seisys.interceptor;

import java.lang.reflect.Method;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.seisys.entity.system.User;
import com.seisys.service.system.user.UserService;
import com.seisys.util.Const;
import com.seisys.util.Jurisdiction;



/**
 * 
 * 类名称：LoginHandlerInterceptor.java 类描述：
 * 
 * @author 
 * @version 1.6
 */
public class LoginHandlerInterceptor extends HandlerInterceptorAdapter {
	
	@Resource(name = "userService")
	private UserService userService;

	private final String ACCESS_TOKEN="access_token";
	
	@Override
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {
		String path = request.getServletPath();
		if (path.matches(Const.NO_INTERCEPTOR_PATH)) {
			return true;
		} else {
			// shiro管理的session
			Subject currentUser = SecurityUtils.getSubject();
			Session session = currentUser.getSession();
			User user = (User) session.getAttribute(Const.SESSION_USER);
			if (user != null) {
				path = path.substring(1, path.length());
				boolean b = Jurisdiction.hasJurisdiction(path);
				if (!b) {
					response.sendRedirect(request.getContextPath()+ Const.LOGIN);
				}
				return b;
			} else {
				// 登陆过滤
				response.sendRedirect(request.getContextPath() + Const.LOGIN);
				return false;
			}
		}
		
		/**jwt验证 开始*/
//		if(!(handler instanceof HandlerMethod)){
//            return true;
//        }
//
//		HandlerMethod handlerMethod = (HandlerMethod)handler;
//		Object bean = handlerMethod.getBean();
//		
//		if(isAnoymous(handlerMethod)){
//            return true;
//        }
//        if(!(bean instanceof BaseController)){ 
//            throw new RuntimeException("must extend basecontroller");
//        }
//        
//        String token = CookieUtil.getCookieValue(request, ACCESS_TOKEN);
//        boolean isAjax = CookieUtil.isAjax(request);
//        if(StringUtils.isEmpty(token)){
//        	if(isAjax){
//        		response.setContentType("text/html;charset=UTF-8");
//                response.getWriter().write("{\"code\":\"-1\",\"msg\":\"error\"}");
//                return false;
//        	}
//        	response.sendRedirect(GpmallWebConstant.GPMALL_SSO_ACCESS_URL);
//        	return false;
//        }
//        CheckAuthRequest checkAuthRequest = new CheckAuthRequest();
//        checkAuthRequest.setToken(token);
//        CheckAuthResponse checkAuthResponse = userService.validToken(checkAuthRequest);
//        if("000000".equals(checkAuthResponse.getCode())){
//            BaseController baseController=(BaseController)bean;
//            baseController.setUid(checkAuthResponse.getUid());
//            return super.preHandle(request, response, handler);
//        }
//        if(isAjax){
//            response.setContentType("text/html;charset=UTF-8");
//            response.getWriter().write("{\"code\":\""+checkAuthResponse.getCode()+"\"" +
//                    ",\"msg\":\""+checkAuthResponse.getMsg()+"\"}");
//            return false;
//        }
//        response.sendRedirect(GpmallWebConstant.GPMALL_SSO_ACCESS_URL);
//        
//        return false;
		/**jwt验证 结束*/
		
	}
	
	
//	private boolean isAnoymous(HandlerMethod handlerMethod){
//        Object bean=handlerMethod.getBean();
//        Class clazz=bean.getClass();
//        if(clazz.getAnnotation(Anoymous.class)!=null){
//            return true;
//        }
//        Method method=handlerMethod.getMethod();
//        return method.getAnnotation(Anoymous.class)!=null;
//    }

}
